var device=request("from")

var page=""//分页

//时间戳转换成时分秒时间
/*
 call:var stamp=timestamp*1000;
 eg:(new Date(stamp)).Format("yyyy-MM-dd hh:mm:ss")      ==> 2016-05-31 08:09:40
 */
Date.prototype.Format = function(fmt) { //author: meizz
	var o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"h+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

$(function(){
	var phone=""
	if(sessionStorage.getItem("phone")!=""&&sessionStorage.getItem("phone")!=undefined&&sessionStorage.getItem("phone")!=null){
		phone=sessionStorage.getItem("phone");
	}
	else{
		var code=request("code");
		$.ajax({
		    url: DATA_URL+"checkwxbind",
		    data: "code="+code,
		    type: "post",
		    async: false,
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		if(data.info.is_bind==1){
		    			phone=data.info.phone;
		    			sessionStorage.setItem("phone",data.info.phone);
		    		}
		    		else{
		    			var open_id=data.info.open_id;
		    			location.href="instructor/binding_phone.html?open_id="+open_id
		    		}
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	}
			
	$(".content").css("min-height",$(window).height()-$(".page_top").outerHeight()-30);
	$(window).resize(function(){
		$(".content").css("min-height",$(window).height()-$(".page_top").outerHeight()-30);
	})
	//提现
	$(".withdrawals").click(function(){
		if(parseFloat($(".page_top .bonus").text())<5){
			$("#prompt").text("您的奖金不足5元，暂无法提现");
            $(".prompt").fadeIn(300,function(){
                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
            })
            return false;
		}
		$("#tixian").show();
	})
	$("#tixian .cancel").click(function(){
		$("#tixian").hide();
		$("#tixian .num input").val("");
	})
	$("#tixian .ok").click(function(){
		if($("#tixian .num input").val().trim()==""){
			$("#prompt").text("请输入提现金额");
            $(".prompt").fadeIn(300,function(){
                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
            })
            return false;
		}
		if(parseFloat($("#tixian .num input").val())>parseFloat($(".page_top .bonus").text())){
			$("#prompt").text("没有足够提现金额");
            $(".prompt").fadeIn(300,function(){
                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
            })
            return false;
		}
		if(parseFloat($("#tixian .num input").val())<20){
			$("#prompt").text("最小提现金额为20元");
            $(".prompt").fadeIn(300,function(){
                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
            })
            return false;
		}
		tixian();
	})
	function tixian(){
		$.ajax({
		    url: DATA_URL+"getcash",
		    data: "phone="+phone+"&money="+$("#tixian .num input").val(),
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		$("#tixian").hide();
		    		$("#prompt").text("提现成功,提现金额:"+$("#tixian .num input").val()+"元");
		    		$("#tixian .num input").val("");
		    		$(".page_top .bonus").text(parseFloat(data.score).toFixed(2))
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	}
	$(".load-more-btn").click(function(){
		get_info();
	})
	get_info();
	function get_info(){
		$.ajax({
		    url: DATA_URL+"scorelist",
		    data: "phone="+phone+"&pubtime="+page+"&page_count=20",
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    beforeSend:function(){
		    	$(".data-loader").show();
		    	$(".load-more-btn").hide();
		    	$(".no-content").hide();
		    },
		    success:function (data) {
		    	$(".data-loader").hide();
		    	if(data.code=="0"){
		    		if(data.info.length>0){
		    			page=data.info[data.info.length-1].pubtime;
		    		}
		    		if(data.info.length>20||data.info.length==20){
		    			$(".load-more-btn").show();
		    		}
		    		else{
		    			$(".load-more-btn").hide();
		    			$(".no-content").show();
		    		}
		    		var info_list="";
		    		var type_txt="";
		    		var change_num="";
		    		var photo_url="";
		    		$(".page_top .bonus").text(parseFloat(data.score).toFixed(2))
		    		$.each(data.info,function(index,val) {
		    			switch(val.type){
		    				case 0:
		    					type_txt="提现";
		    					change_num="<span class='green'>-"+val.score+"</span>";
		    					photo_url="images/bonus_02.png";
		    				break;
		    				case 1:
		    					type_txt="邀请好友："+val.nick_name;
		    					change_num="<span class='red'>+"+val.score+"</span>";
		    					photo_url=val.avatar;
		    				break;
		    				case 2:
		    					type_txt="分享奖励";
		    					change_num="<span class='red'>+"+val.score+"</span>";
		    					photo_url="images/bonus_01.png";
		    				break;
		    				case 3:
		    					type_txt="系统奖励";
		    					change_num="<span class='red'>+"+val.score+"</span>";
		    					photo_url="images/bonus_04.png";
		    				break;
		    				case 4:
		    					type_txt="系统扣除";
		    					change_num="<span class='green'>-"+val.score+"</span>";
		    					photo_url="images/bonus_03.png";
		    				break;
		    			}
		    			info_list+="<tr>\
										<td width='50'>\
											<div class='photo' style='background-image: url("+photo_url+");'></div>\
										</td>\
										<td>\
											<div class='text'>"+type_txt+"</div>\
											<div class='date'>"+(new Date(val.pubtime * 1000)).Format("yyyy-MM-dd hh:mm")+"</div>\
										</td>\
										<td width='80'>\
											<div class='change_num'>"+change_num+"</div>\
										</td>\
									</tr>"
		    		});
		    		$(".content .list").append(info_list)
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	}
})
